// (function () {
//     var obj = {};
//     obj.ajax = function (type, url, data, headers, context, callback) {
//         $.ajax({type: type, url: url, data: JSON.stringify(data), headers: headers, context: context})
//             .done(callback);
//     };
//     window.j = obj;
// })();

function commentsComponent(template) {
    var model = [];
    var starList = [];
    var template = template;

    this.loadAllComment = function (id) {
        j.ajaxWithOutJson("GET", "getOrderByMerchantId", {id: id}, null, this, function (m) {
            model = m;
            j.ajaxWithOutJson("GET", "showStar", {id: id}, null, this, function (getStar) {
                starList = getStar;
                _render_comment(starList, model);
            });
        });
    };

    function _render_comment(starList, data) {
        template.empty();
        var flag = 0;
        data.forEach(function (item, index) {
            console.log(item.customer.name);
            var clone = $(".item").clone().removeClass("item").removeClass("hidden-attr").addClass("item_comment");
            /*顾客名称*/
            clone.children("div:nth-child(1)").children("span:nth-child(1)").text(item.customer.name);
            /*星星*/
            var s = clone.children("div:nth-child(1)").children("span:nth-child(2)").children("div:nth-child(1)").children("span");
            var t = 0;
            if(item.star){
                s.each(function () {
                    if (parseInt($(this).attr("id")) <= item.star)
                        $(this).addClass("star-color");
                        flag++;
                });
            }

            /*日期*/
            clone.children("div:nth-child(1)").children("span:nth-child(2)").children("div:nth-child(2)").children("span").text(getMyDate(item.createDate));

            if (item.comment.length == 0) {
                /*顾客评论*/
                clone.children("div:nth-child(2)").children("p:nth-child(1)").text("该用户没有评价");
            }
            if (item.comment.length == 1) {
                /*顾客评论*/
                clone.children("div:nth-child(2)").children("p:nth-child(1)").text("用户：" + item.comment[0].content);
            }
            if (item.comment.length > 1) {
                /*商家回复*/
                clone.children("div:nth-child(2)").children("p:nth-child(1)").text("用户：" + item.comment[0].content);
                clone.children("div:nth-child(2)").children("p:nth-child(2)").text("商家：" + item.comment[1].content);
            }
            clone.appendTo(template);
        });
    }

    function getzf(num) {
        if (parseInt(num) < 10) {
            num = '0' + num;
        }
        return num;
    }

    function getMyDate(str) {
        var oDate = new Date(str),
            oYear = oDate.getFullYear(),
            oMonth = oDate.getMonth() + 1,
            oDay = oDate.getDate(),
            oHour = oDate.getHours(),
            oMin = oDate.getMinutes(),
            oSen = oDate.getSeconds(),
            oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen);//最后拼接时间
        return oTime;
    }
}

$(function () {
    var c = new commentsComponent($("#show-merchant-comment-area"));
    var param = getUrlParameter("id");
    console.log(param);
    c.loadAllComment(param);
});
	